/*
  Copyright 2009 Colin Prinn

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package net.recursv.motific.unittest;

import java.io.File;
import java.util.Arrays;
import java.util.Vector;

/**
 * Created by: recursv
 * Date: 1/08/2009
 * Time: 3:22:16 PM
 */
public class Main {

    private static String[] initClasspath(Vector<String> params) {
        String[] classpath = null;

        int index = params.indexOf("-cp");
        if (index != -1) {
            try {
                String value = params.get(index + 1);
                classpath = value.split(File.pathSeparator);

                // Remove classpath options
                params.remove("-cp");
                params.remove(value);
            } catch (ArrayIndexOutOfBoundsException e) {
                System.out.println("-cp requires class path specification");
                usage();
            }
        }

        return classpath;
    }

    private static File initInputFile(Vector<String> params) {
        File inputFile = null;

        if (params.size() == 1) {
            String value = params.get(0).trim();
            inputFile = new File(value);
            if (inputFile.exists()) {
                params.remove(0);
            } else {
                System.out.println("File \"" + value + "\" does not exists.");
                usage();
            }
        } else {
            System.out.println("Invalid number of parameters.");
            usage();
        }

        return inputFile;
    }

    private static File initOutputFile(Vector<String> params) {
        File outputFile = null;

        int index = params.indexOf("-o");
        if (index != -1) {
            String value = params.get(index + 1);

            outputFile = new File(value.trim());

            params.remove(index);
            params.remove(value);
        }

        return outputFile;
    }

    public static void main(String[] args) {
        Vector<String> params = new Vector<String>(Arrays.asList(args));

        String[] classpath = initClasspath(params);
        File outputFile = initOutputFile(params);
        File inputFile = initInputFile(params);

        if (outputFile == null)
            outputFile = inputFile;

        Weaver weaver = new Weaver();

        try {
            weaver.setClasspath(classpath);
            weaver.setOutputFile(outputFile);
            weaver.setInputFile(inputFile);
            weaver.execute();
        } catch (Weaver.WeaverException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }

    private static void usage() {
        System.out.println("Usage:\n" +
                "java " + Main.class.getName() + " [-options] jarfile | zipfile | directory\n\n" +
                "Where options are:\n" +
                "-cp\t<Classpath of directories and zip/jar files separeted by " + File.pathSeparatorChar + ">\n" +
                "-o\t<Output directory where files will be stored. If this is omitted, the source files will be overwritten which is probably what you want>\n");
        System.exit(1);
    }
}